package com.jiezuo.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiezuo.auth.entity.Permission;
import com.jiezuo.auth.entity.Role;
import com.jiezuo.auth.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface RoleMapper extends BaseMapper<Role> {

    //通过用户ID查询所有角色
//    @Select("SELECT DISTINCT " +
//            " r.id, " +
//            " r.role_name, " +
//            " r.description " +
//            "FROM sys_user_role ru " +
//            "LEFT JOIN sys_role r ON r.id=ru.role_id " +
//            "WHERE ru.user_id=#{userId} ORDER BY r.id  ASC")
//    List<Role> findRolesByUserId(String userId);

    //通过用户ID查询所有角色
    @Select("SELECT DISTINCT  " +
            "  r.id,  " +
            "  r.role_name,  " +
            "  r.data_authority_level,  " +
            "  r.description  " +
            "FROM sys_role r  " +
            "LEFT JOIN sys_user_role ru ON r.id=ru.role_id AND r.is_delete=0 " +
            "WHERE ru.user_id=#{userId} AND r.id IS NOT NULL ORDER BY r.id ASC ")
    List<Role> findRolesByUserId(String userId);

    List<Permission> findPermissionsByRoleId(@Param("role_id") String role_id);

    List<String> findCodeByUserId(@Param("user_id") String user_id);

//    @Select(" SELECT sd.* FROM sys_depart_role sdr " +
//            " LEFT JOIN sys_depart sd ON sdr.depart_id=sd.id " +
//            " WHERE sdr.role_id=#{role_id}")
//    List<Depart> findDepartsById(@Param("role_id") String role_id);

    @Select("SELECT su.user_code, su.user_name, su.real_name,su.status FROM sys_user_role sur " +
            "LEFT JOIN sys_user su ON su.id=sur.user_id " +
            "WHERE sur.role_id=#{role_id} AND su.is_delete=0 AND su.id IS NOT NULL")
    List<User> findUsersByRoleId(@Param("role_id") String role_id);

}
